package Monitors.Sequencecontrol.固定运行顺序;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j(topic = "c.awaitsignal")
public class awaitsignal {
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();
    // t2 运行标记， 代表 t2 是否执行过
    static boolean t2runed = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {

                // 如果 t2 没有执行过
             try {
                 lock.lock();
                 while (!t2runed) {
                     try {
                         // t1 先等一会
                         condition.await();
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
             }finally {
                 lock.unlock();
             }

            System.out.println(1);
        });
        Thread t2 = new Thread(() -> {
            System.out.println(2);
            try {
                lock.lock();
                t2runed = true;
                condition.signal();
            }finally {
                lock.unlock();
            }


                // 修改运行标记

                // 通知 obj 上等待的线程（可能有多个，因此需要用 notifyAll）

        });
        t1.start();
        t2.start();
    }
}
